#!/usr/bin/env python
# encoding: utf-8

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from ui.DeviceInput import Ui_Dialog as DeviceInputDialog
from dbsession import SqlDeal,Data,Condition,Device,session
class DeviceInputDlg(QDialog, DeviceInputDialog):
    def __init__(self,parent=None):
        super(DeviceInputDlg, self).__init__()
        self.setupUi(self)
        self.setWindowTitle('设备数据库管理')
        self.pushButton_ok.clicked.connect(self.accept)
        self.pushButton_cancel.clicked.connect(self.reject)
        self.pushButton_create.clicked.connect(self.create_device)
        self.pushButton_update.clicked.connect(self.update_device)
        self.pushButton_delete.clicked.connect(self.delete_device)
        # 表格model控件
        self.model = QStandardItemModel(0, 0)
        self.HeaderList = ['ID','设备','房间','备注']
        self.model.setHorizontalHeaderLabels(self.HeaderList)
        # 去掉行头显示
        self.tableView.resizeRowsToContents()
        self.tableView.resizeColumnsToContents()
        self.tableView.setModel(self.model)
        self.tableView.clicked.connect(self.tableClicked)
        self.tableView.verticalHeader().hide()
        self.tableView.horizontalHeader().setStretchLastSection(True)
        self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.tableView.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.tableView.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.tableView.setSelectionMode( QAbstractItemView.SingleSelection)
        # 数据库
        self.sql = SqlDeal()
        # 更新表格数据
        self.tableUpdate()
        self.select_ID = 0

    def update_device(self):
        name = self.lineEdit_device.text()
        room = self.lineEdit_room.text()
        remark = self.lineEdit_remark.text()
        if self.select_ID > 0:
            dev = self.sql.get_device_by_id_sql(self.select_ID)
            if dev:
                self.sql.update_device_sql(name,room,remark,self.select_ID)
                self.tableUpdate()
            else:
                QMessageBox(self,'警告','设备不存在，无法修改!',QMessageBox.Yes)

    def create_device(self):
        name = self.lineEdit_device.text()
        room = self.lineEdit_room.text()
        remark = self.lineEdit_remark.text()
        if name:
            dev = self.sql.get_device_by_name_sql(name)
            if dev:
                QMessageBox.information(self,'警告','设备已经存在!',QMessageBox.Yes)
            else:
                self.sql.insert_device_sql(name=name,room=room,remark=remark)
                self.tableUpdate()
        else:
            QMessageBox.information(self,'警告','请填写信息!',QMessageBox.Yes)

    def delete_device(self):
        if not QMessageBox.warning(self,'警告','确定要删除此加速器以及加速器下所有的测量信息？', QMessageBox.Yes|QMessageBox.No) == 65536:
            sql = ''' select c.id from device d,condition c where d.id=c.device_id and d.id=%s'''%self.select_ID
            condition_id = session.execute(sql).fetchall()
            for _id in condition_id:
                sql = '''delete from data where data.condition_id=%s '''%_id[0]
                res = session.execute(sql)
            sql = '''delete from condition where condition.device_id=%s '''%self.select_ID
            res = session.execute(sql)
            sql = '''delete from device where device.id=%s '''%self.select_ID
            res = session.execute(sql)
            session.commit()
            self.tableUpdate()
            QMessageBox.information(self,'提示','删除成功', QMessageBox.Yes)

    def tableClear(self):
        self.model.clear()
        self.model = QStandardItemModel(0, 0)
        self.model.setHorizontalHeaderLabels(self.HeaderList)
        self.tableView.setModel(self.model)

    def tableUpdate(self):
        self.tableClear()
        dev = self.sql.get_device_sql()
        for i in range(len(dev)):
            self.model.setItem(i, 0, QStandardItem(str(dev[i]['id'])))
            self.model.setItem(i, 1, QStandardItem(str(dev[i]['name'])))
            self.model.setItem(i, 2, QStandardItem(str(dev[i]['room'])))
            self.model.setItem(i, 3, QStandardItem(str(dev[i]['remark'])))

    def tableClicked(self,index):
        row = index.row()
        self.select_ID = int(self.model.data(self.model.index(row,0)))
        self.lineEdit_device.setText(self.model.data(self.model.index(row,1)))
        self.lineEdit_room.setText(self.model.data(self.model.index(row,2)))
        self.lineEdit_remark.setText(self.model.data(self.model.index(row,3)))

    def save_data(self,task,data):
        '''保存测量的一组数据数据'''
        # data = oar_data
        if self.select_ID > 0:
            condition_id = self.sql.insert_condition_data(self.select_ID,task)
            if condition_id:
                data_id = self.sql.insert_data(condition_id,data)

    def accept(self):
        if self.select_ID > 0:
            QDialog.accept(self)
        else:
            QMessageBox.warning(self,'提示','请选择加速器',QMessageBox.Yes)

    def reject(self):
        QDialog.reject(self)
